{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "nasty-intention",
   "metadata": {
    "id": "nasty-intention"
   },
   "source": [
    "# Colab Demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "previous-bacon",
   "metadata": {
    "id": "previous-bacon"
   },
   "source": [
    "## Dependencies and Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "complicated-receiver",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T06:34:38.757647Z",
     "start_time": "2021-06-18T06:34:37.545968Z"
    },
    "cellView": "form",
    "id": "complicated-receiver"
   },
   "outputs": [],
   "source": [
    "#@title Install dependencies\n",
    "\n",
    "!pip install -q omegaconf\n",
    "\n",
    "import torch\n",
    "from pprint import pprint\n",
    "from omegaconf import OmegaConf\n",
    "from IPython.display import Audio, display\n",
    "\n",
    "torch.hub.download_url_to_file('https://raw.githubusercontent.com/snakers4/silero-models/master/models.yml',\n",
    "                               'latest_silero_models.yml',\n",
    "                               progress=False)\n",
    "models = OmegaConf.load('latest_silero_models.yml')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7oQde2ZNE-ok",
   "metadata": {
    "id": "7oQde2ZNE-ok"
   },
   "source": [
    "## List models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pacific-injury",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-18T06:34:47.677954Z",
     "start_time": "2021-06-18T06:34:47.665753Z"
    },
    "id": "pacific-injury"
   },
   "outputs": [],
   "source": [
    "# see latest avaiable models\n",
    "available_languages = list(models.tts_models.keys())\n",
    "print(f'Available languages {available_languages}')\n",
    "\n",
    "for lang in available_languages:\n",
    "    _models = list(models.tts_models.get(lang).keys())\n",
    "    print(f'Available models for {lang}: {_models}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b37b3d0",
   "metadata": {},
   "source": [
    "## V5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71bebc98",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "language = 'ru'\n",
    "model_id = 'v5_ru'\n",
    "device = torch.device('cpu')\n",
    "\n",
    "model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models',\n",
    "                                     model='silero_tts',\n",
    "                                     language=language,\n",
    "                                     speaker=model_id)\n",
    "model.to(device)  # gpu or cpu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4782713d",
   "metadata": {},
   "source": [
    "### Speakers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e8afb06",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.speakers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b7cf618",
   "metadata": {},
   "source": [
    "### Text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6875417c",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 48000\n",
    "speaker = 'xenia'\n",
    "put_accent=True\n",
    "put_yo=True\n",
    "put_stress_homo=True\n",
    "put_yo_homo=True\n",
    "\n",
    "example_text = 'Меня зовут Лева Королев. Я из готов. И я уже готов открыть все ваши замки любой сложности!'\n",
    "\n",
    "audio = model.apply_tts(text=example_text,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate,\n",
    "                        put_accent=put_accent,\n",
    "                        put_yo=put_yo,\n",
    "                        put_stress_homo=put_stress_homo,\n",
    "                        put_yo_homo=put_yo_homo)\n",
    "print(example_text)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0ce7df5",
   "metadata": {},
   "source": [
    "### SSML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9e159a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "ssml_sample = \"\"\"\n",
    "              <speak>\n",
    "              <p>\n",
    "                  Когда я просыпаюсь, <prosody rate=\"x-slow\">я говорю довольно медленно</prosody>.\n",
    "                  Пот+ом я начинаю говорить своим обычным голосом,\n",
    "                  <prosody pitch=\"x-high\"> а могу говорить тоном выше </prosody>,\n",
    "                  или <prosody pitch=\"x-low\">наоборот, ниже</prosody>.\n",
    "                  Пот+ом, если повезет – <prosody rate=\"fast\">я могу говорить и довольно быстро.</prosody>\n",
    "                  А еще я умею делать паузы любой длины, например, две секунды <break time=\"2000ms\"/>.\n",
    "                  <p>\n",
    "                    Также я умею делать паузы между параграфами.\n",
    "                  </p>\n",
    "                  <p>\n",
    "                    <s>И также я умею делать паузы между предложениями</s>\n",
    "                    <s>Вот например как сейчас</s>\n",
    "                  </p>\n",
    "              </p>\n",
    "              </speak>\n",
    "              \"\"\"\n",
    "\n",
    "sample_rate = 48000\n",
    "speaker = 'xenia'              \n",
    "audio = model.apply_tts(ssml_text=ssml_sample,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aebc6429",
   "metadata": {},
   "source": [
    "## V4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14f49758",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "language = 'ru'\n",
    "model_id = 'v4_ru'\n",
    "device = torch.device('cpu')\n",
    "\n",
    "model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models',\n",
    "                                     model='silero_tts',\n",
    "                                     language=language,\n",
    "                                     speaker=model_id)\n",
    "model.to(device)  # gpu or cpu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98a29010",
   "metadata": {},
   "source": [
    "### Speakers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ae5f9dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.speakers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6beca794",
   "metadata": {},
   "source": [
    "### Text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bab211a",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 48000\n",
    "speaker = 'xenia'\n",
    "put_accent=True\n",
    "put_yo=True\n",
    "example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра к+едров.'\n",
    "\n",
    "audio = model.apply_tts(text=example_text,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate,\n",
    "                        put_accent=put_accent,\n",
    "                        put_yo=put_yo)\n",
    "print(example_text)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a1ab3a",
   "metadata": {},
   "source": [
    "### SSML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c29189f",
   "metadata": {},
   "outputs": [],
   "source": [
    "ssml_sample = \"\"\"\n",
    "              <speak>\n",
    "              <p>\n",
    "                  Когда я просыпаюсь, <prosody rate=\"x-slow\">я говорю довольно медленно</prosody>.\n",
    "                  Пот+ом я начинаю говорить своим обычным голосом,\n",
    "                  <prosody pitch=\"x-high\"> а могу говорить тоном выше </prosody>,\n",
    "                  или <prosody pitch=\"x-low\">наоборот, ниже</prosody>.\n",
    "                  Пот+ом, если повезет – <prosody rate=\"fast\">я могу говорить и довольно быстро.</prosody>\n",
    "                  А еще я умею делать паузы любой длины, например, две секунды <break time=\"2000ms\"/>.\n",
    "                  <p>\n",
    "                    Также я умею делать паузы между параграфами.\n",
    "                  </p>\n",
    "                  <p>\n",
    "                    <s>И также я умею делать паузы между предложениями</s>\n",
    "                    <s>Вот например как сейчас</s>\n",
    "                  </p>\n",
    "              </p>\n",
    "              </speak>\n",
    "              \"\"\"\n",
    "\n",
    "sample_rate = 48000\n",
    "speaker = 'xenia'              \n",
    "audio = model.apply_tts(ssml_text=ssml_sample,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66fc3785",
   "metadata": {},
   "source": [
    "### Random speaker"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d304ba2d",
   "metadata": {},
   "source": [
    "#### Generate new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93115390",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 48000\n",
    "speaker = 'random'\n",
    "\n",
    "example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра к+едров.'\n",
    "\n",
    "audio = model.apply_tts(text=example_text,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate)\n",
    "print(example_text)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30ecc807",
   "metadata": {},
   "source": [
    "#### Save generated speaker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "013ca793",
   "metadata": {},
   "outputs": [],
   "source": [
    "voice_path = 'test_voice.pt'\n",
    "model.save_random_voice(voice_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afd37773",
   "metadata": {},
   "source": [
    "#### Generate with saved speaker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b27ff26",
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 48000\n",
    "speaker = 'random'\n",
    "voice_path = 'test_voice.pt'\n",
    "\n",
    "example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра к+едров.'\n",
    "\n",
    "audio = model.apply_tts(text=example_text,\n",
    "                        speaker=speaker,\n",
    "                        sample_rate=sample_rate,\n",
    "                        voice_path=voice_path)\n",
    "print(example_text)\n",
    "display(Audio(audio, rate=sample_rate))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "Pwg_AYTCmufA",
   "metadata": {
    "id": "Pwg_AYTCmufA"
   },
   "source": [
    "# Minimal Example to Run Locally "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "Phw8EAVBJGW8",
   "metadata": {
    "id": "Phw8EAVBJGW8"
   },
   "source": [
    "## Dependencies and Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pLTZK0O7JPHT",
   "metadata": {
    "cellView": "form",
    "id": "pLTZK0O7JPHT"
   },
   "outputs": [],
   "source": [
    "#@title Install dependencies\n",
    "\n",
    "!pip install -q torch==1.12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20cf87d9",
   "metadata": {},
   "source": [
    "## V5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "832b0ceb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import torch\n",
    "\n",
    "device = torch.device('cpu')\n",
    "torch.set_num_threads(4)\n",
    "local_file = 'model.pt'\n",
    "\n",
    "if not os.path.isfile(local_file):\n",
    "    torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v5_ru.pt',\n",
    "                                   local_file)  \n",
    "\n",
    "model = torch.package.PackageImporter(local_file).load_pickle(\"tts_models\", \"model\")\n",
    "model.to(device)\n",
    "\n",
    "example_text = 'Меня зовут Лева Королев. Я из готов. И я уже готов открыть все ваши замки любой сложности!'\n",
    "sample_rate = 48000\n",
    "speaker='baya'\n",
    "\n",
    "audio_paths = model.save_wav(text=example_text,\n",
    "                             speaker=speaker,\n",
    "                             sample_rate=sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b9e704a",
   "metadata": {},
   "source": [
    "## V4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8281068",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import torch\n",
    "\n",
    "device = torch.device('cpu')\n",
    "torch.set_num_threads(4)\n",
    "local_file = 'model.pt'\n",
    "\n",
    "if not os.path.isfile(local_file):\n",
    "    torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v4_ru.pt',\n",
    "                                   local_file)  \n",
    "\n",
    "model = torch.package.PackageImporter(local_file).load_pickle(\"tts_models\", \"model\")\n",
    "model.to(device)\n",
    "\n",
    "example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра кедров.'\n",
    "sample_rate = 48000\n",
    "speaker='baya'\n",
    "\n",
    "audio_paths = model.save_wav(text=example_text,\n",
    "                             speaker=speaker,\n",
    "                             sample_rate=sample_rate)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "examples_tts.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
